package com.dc.project_kotlin.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.dc.project_kotlin.R
import com.dc.project_kotlin.adapter.ArticleAdapter
import com.dc.project_kotlin.adapter.HomeBannerAdapter
import com.dc.project_kotlin.base.BaseFragment
import com.dc.project_kotlin.bean.BannerResponse
import com.dc.project_kotlin.databinding.FragmentHomeBinding
import com.dc.project_kotlin.ext.loadServiceInit
import com.dc.project_kotlin.ext.nav
import com.dc.project_kotlin.ext.navigateAction
import com.dc.project_kotlin.loadcallback.LoadingCallback
import com.dc.project_kotlin.vm.HomeViewModel
import com.dc.project_kotlin.weight.banner.HomeBannerViewHolder
import com.kingja.loadsir.core.LoadService
import com.zhpan.bannerview.BannerViewPager

class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>() {

    private lateinit var loadService: LoadService<Any>

    private lateinit var articleAdapter: ArticleAdapter

    override fun initView(view: View) {
        loadService = loadServiceInit(mViewBinding.includeList.includeRecyclerview.swipeRefresh) {
            loadService.showCallback(LoadingCallback::class.java)
        }
        mViewBinding.includeToolbar.toolbar.run {
            title = "首页"
            inflateMenu(R.menu.home_menu)
            setOnMenuItemClickListener {
                when (it.itemId) {
                    R.id.home_search -> {
                        nav().navigateAction(R.id.action_mainfragment_to_searchFragment)
                    }
                }
                true
            }
        }
        articleAdapter = ArticleAdapter(mActivity, mutableListOf(), true)
        mViewBinding.includeList.includeRecyclerview.recyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = articleAdapter
        }
        articleAdapter.run {
            setCollectClick { item, v, position ->
                if (v.isChecked) {

                } else {

                }
            }

            setItemClickListener(object : ItemClickListener {
                override fun onItemClick(position: Int) {

                }

            })
        }
    }

    override fun initData() {

    }

    override fun createObserver() {
        mViewModel.run {
            bannerLiveData.observe(viewLifecycleOwner, Observer {
                val data = it
                if (mViewBinding.includeList.includeRecyclerview.recyclerView.headerCount == 0) {
                    val headerView =
                        LayoutInflater.from(context).inflate(R.layout.include_banner, null)
                            .apply {
                                findViewById<BannerViewPager<BannerResponse, HomeBannerViewHolder>>(
                                    R.id.banner_view
                                ).apply {
                                    adapter = HomeBannerAdapter()
                                    setLifecycleRegistry(lifecycle)
                                    setOnPageClickListener {
                                        nav().navigateAction(
                                            R.id.action_to_webFragment,
                                            Bundle().apply {
                                                putParcelable(
                                                    "bannerdata",
                                                    data[it]
                                                )
                                            })
                                    }
                                    create(data)
                                }
                            }
                    mViewBinding.includeList.includeRecyclerview.recyclerView.addHeaderView(headerView)
                }
            })
        }
    }


    override fun lazyLoad() {
        loadService.showCallback(LoadingCallback::class.java)
        mViewModel.getBannerData()
    }
}